// pages/productlist/productlist.js
const product = require('../../api/product.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    product: [],
    typeid: '',
    name: '',
    value: '',
    show: false,
    isSelect1: false,
    isSelect2: false,
    isSelect3: false,
    isSelect4: false,
    column: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    console.log(options)
    if (options.typeid) {
      this.setData({
        typeid: options.typeid
      })
    } else if (options.name) {
      this.setData({
        name: options.name
      })
    }
    wx.showLoading({
      title: '正在加载',
    })
  },
  // 根据商品类别获取商品
  getProductsByCategory() {
    product.getProductsByCategory(this.data.typeid).then(res => {
      if (res.data.code == 0) {
        this.setData({
          product: res.data.data
        })
        wx.hideLoading()
      }
    })
  },
  // 根据搜索商品名称获取商品
  getProductsByKeyWords() {
    const query = {
      name: this.data.name
    }
    product.getProductsByKeyWords(query).then(res => {
      console.log(res)
      this.setData({
        product: res.data.data
      })
      wx.hideLoading()
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    if (this.data.typeid) {
      this.getProductsByCategory()
    } else if (this.data.name) {
      this.getProductsByKeyWords()
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  productDetail: function(e) {
    let id = e.target.dataset.id
    wx.navigateTo({
      url: '../productdetail/productdetail?id=' + id,
    })
  },
  onChange(e) {
    this.setData({
      value: e.detail
    })
  },
  onSearch() {
    if (this.data.value) {
      this.setData({
        product: [],
        name: '',
        typeid: ''
      })
      wx.showLoading({
        title: '正在加载',
      })
      const query = {
        name: this.data.value
      }
      product.getProductsByKeyWords(query).then(res => {
        if (res.data.code == 0) {
          this.setData({
            product: res.data.data
          })
          wx.hideLoading()
        }
      })
    }else {
      wx.showToast({
        title: '关键字不能为空',
        icon: 'none'
      })
    }
  },
  toShowFilter() {
    this.setData({ show: true })
  },
  onClose() {
    this.setData({ show: false })
  },
  toFilter(e) {
    console.log(e)
    let column = e.currentTarget.dataset.column
    this.setData({
      column: column
    })
    switch (column) {
      case 'high_price':
        this.setData({
          isSelect1: true,
          isSelect2: false,
          isSelect3: false,
          isSelect4: false
        })
      break
      case 'lower_price':
        this.setData({
          isSelect2: true,
          isSelect1: false,
          isSelect3: false,
          isSelect4: false
        })
      break
      case 'high_sale':
        this.setData({
          isSelect3: true,
          isSelect1: false,
          isSelect2: false,
          isSelect4: false
        })
      break
      case 'lower_sale':
        this.setData({
          isSelect4: true,
          isSelect1: false,
          isSelect3: false,
          isSelect2: false
        })
      break
    }
    console.log(this.data.column)
  },
  cancel() {
    this.setData({
      isSelect1: false,
      isSelect2: false,
      isSelect3: false,
      isSelect4: false,
      column: '',
      product: []
    })
    wx.showLoading({
      title: '正在加载',
    })
    let query = {}
    if(this.data.value) {
      this.setData({
        name: this.data.value
      })
    }
    if(this.data.name) {
      query.name = this.data.name
      product.getProductsByKeyWords(query).then(res => {
        if (res.data.code == 0) {
          this.setData({
            product: res.data.data,
            show: false
          })
          wx.hideLoading()
        }
      })
    }else if(this.data.typeid) {
      product.getProductsByCategory(this.data.typeid).then(res => {
        if (res.data.code == 0) {
          this.setData({
            product: res.data.data,
            show: false
          })
          wx.hideLoading()
        }
      })
    }
  },
  submit() {
    this.setData({
      show: false,
      product: []
    })
    if(this.data.value) {
      this.setData({
        name: this.data.value
      })
    }
    console.log(this.data.name, this.data.typeid, this.data.column)
    product.getProductsByFilter(this.data.name, this.data.typeid, this.data.column).then(res => {
      if(res.data.code == 0) {
        this.setData({
          product: res.data.data,
          show: false
        })
      }
    })
  } 
})